package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.sikao.app.R;
import com.sikao.app.adapter.AuctionOrderListAdapter;
import com.sikao.app.adapter.InspectionReportForAuctionAdapter;
import com.sikao.app.adapter.ProductExtraInfAdapter;
import com.sikao.app.bean.AddCollectBean;
import com.sikao.app.bean.AddCollectSubmitBean;
import com.sikao.app.bean.AuctionDetailBean;
import com.sikao.app.bean.AuctionDetailSubmitBean;
import com.sikao.app.bean.AuctionLogListBean;
import com.sikao.app.bean.AuctionLogListSubmitBean;
import com.sikao.app.bean.CancelCollectBean;
import com.sikao.app.bean.CancelCollectSubmitBean;
import com.sikao.app.bean.CheckCollectBean;
import com.sikao.app.bean.CheckCollectSubmitBean;
import com.sikao.app.bean.ProductIndexBean;
import com.sikao.app.bean.ProductIndexListBeans;
import com.sikao.app.dao.AddCollectModel;
import com.sikao.app.dao.AuctionDetailModel;
import com.sikao.app.dao.AuctionLogListModel;
import com.sikao.app.dao.CancelCollectModel;
import com.sikao.app.dao.CheckCollectModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.ConstantClass;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.view.CountDownLinearLayout;
import com.sikao.app.view.MyGridView;
import com.sikao.app.view.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 拍卖详情页
 */
public class AuctionDetailsActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;

	private TextView tvGoodsName;
	private TextView tvCanSplit;
	private TextView tvAmount;
	private TextView tvAmountUnit;
	private TextView tvGoodsPrice;
	private TextView tvOfferTimes;
	private TextView tvStartPrice;
	private TextView tvDeliveryType;
	private TextView tvAuctionMode;
	private TextView tvTransportMethod;
	private TextView tvGoodsType;
	private TextView tvPayMethod;
	private TextView tvMarkupPrice;
	private LinearLayout linearlayoutDealPrice;
	private TextView tvDealPrice;
	private CountDownLinearLayout cdll;//倒计时控件
	private MyListView productIndexListView;
	private MyGridView inspectionReportGridView;
	private MyListView listviewAucList;//出价记录
	private Button btnCollect;
	private Button orderBtn;

	private PullToRefreshScrollView mPullRefreshScrollView;
	String auctionId = "";
	AuctionDetailBean auctionDetailBean;

	private ProductExtraInfAdapter productExtraInfAdapter;
	private InspectionReportForAuctionAdapter inspectionReportForAuctionAdapter;//质检报告
	private AuctionOrderListAdapter auctionOrderListAdapter;//要修改 出价记录


	//产品指标
	private List<ProductIndexBean> productIndexBeanList = new ArrayList<ProductIndexBean>();
	private List<ProductIndexListBeans> productIndexListBeanses = new ArrayList<>();
	//投标记录  bean要修改成最新的
	List<AuctionLogListBean.ResBean.ListBean> autionOrderList = new ArrayList<AuctionLogListBean.ResBean.ListBean>();
	//质检报告
	private List<AuctionDetailBean.ResBean.CheckReportInfoBean> check_report = new ArrayList<>();

	//是否收藏
	private boolean isCollected = false;
	//收藏的id
	private String collectId = "0";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_auction_details);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void init() {
		titleTv.setText("拍卖详情");
		backIv.setVisibility(View.VISIBLE);

		//productIndexAdapter = new ProductIndexAdapter(this, productIndexBeanList);
		//productIndexGridView.setAdapter(productIndexAdapter);
		productExtraInfAdapter = new ProductExtraInfAdapter(this, productIndexListBeanses);
		productIndexListView.setAdapter(productExtraInfAdapter);

		inspectionReportForAuctionAdapter = new InspectionReportForAuctionAdapter(this, check_report);
		inspectionReportGridView.setAdapter(inspectionReportForAuctionAdapter);

		auctionOrderListAdapter = new AuctionOrderListAdapter(this, autionOrderList);
		listviewAucList.setAdapter(auctionOrderListAdapter);
		//获取拍卖详情
		getAuctionDetails(true);
		//获取拍卖记录
		getAuctionOrderList(false);


		if (ConstantClass.state == 1) {//如果已经登录检查是否收藏
			checkCollect(false);
		}

	}

	
	
	
	private void checkCollect(boolean flag) {
		CheckCollectSubmitBean checkCollectSubmitBean = new CheckCollectSubmitBean();
		checkCollectSubmitBean.setObjId(auctionId);
		checkCollectSubmitBean.setType("auction");

		new CheckCollectModel(this, new OnNetBackListener<CheckCollectBean>() {
			@Override
			public void netBack(CheckCollectBean data) {
				if (data == null) {
					Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						if ("1".equals(data.getRes().getStatus())) {//已经收藏
							isCollected = true;
							btnCollect.setText("已收藏");
							collectId = data.getRes().getId();
						} else {
							isCollected = false;
							btnCollect.setText("收藏");
						}
					} else {
						Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}).request(JSONUtil.toJSON(checkCollectSubmitBean), flag);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (ConstantClass.state == 1) {//如果已经登录检查是否收藏
			checkCollect(true);
		}
	}

	private void initViewData() {

		tvGoodsName.setText(auctionDetailBean.getRes().getTitle());
		//  tvCanSplit.setText(auctionDetailBean.getRes().ge);
		tvAmount.setText(auctionDetailBean.getRes().getNumber());
		tvAmountUnit.setText(auctionDetailBean.getRes().getUnit());
		tvGoodsPrice.setText(auctionDetailBean.getRes().getPrice());
		tvOfferTimes.setText(auctionDetailBean.getRes().getView_times());
		tvStartPrice.setText(auctionDetailBean.getRes().getTotal_price());
		tvDeliveryType.setText(auctionDetailBean.getRes().getDelivery_type_str());
		// tvAuctionMode.setText(auctionDetailBean.getRes().get);
		//  tvTransportMethod.setText(auctionDetailBean.getRes().ge);
		tvGoodsType.setText(auctionDetailBean.getRes().getCat_name());
		tvPayMethod.setText(auctionDetailBean.getRes().getPay_type_str() + "");
		tvMarkupPrice.setText(auctionDetailBean.getRes().getLeast_add());

		tvDealPrice.setText(auctionDetailBean.getRes().getFixed_price());
		long endTime = Long.parseLong(TextUtils.isEmpty(auctionDetailBean.getRes().getEnd_time()) ? "0" : auctionDetailBean.getRes().getEnd_time());
		cdll.startCountDown(endTime);//倒计时控件


		//产品指标
		List<AuctionDetailBean.ResBean.GoodsAttrsBean> getGoods_attrs = auctionDetailBean.getRes().getGoods_attrs();
		productIndexBeanList.clear();
		productIndexListBeanses.clear();
		if (getGoods_attrs != null && getGoods_attrs.size() > 0) {

			for (int i = 0; i < getGoods_attrs.size(); i++) {
				//if (!("3".equals(getGoods_attrs.get(i).getAttr_type())) && !("5".equals(getGoods_attrs.get(i).getAttr_type()))&& !("".equals(getGoods_attrs.get(i).getAttr_type()))&& getGoods_attrs.get(i).getAttr_type()!=null) {
				if (!("".equals(getGoods_attrs.get(i).getAttr_type())) && getGoods_attrs.get(i).getAttr_type() != null) {
					ProductIndexBean productIndexBean = new ProductIndexBean();
					productIndexBean.setId(getGoods_attrs.get(i).getId());
					productIndexBean.setName(getGoods_attrs.get(i).getName());
					productIndexBean.setDefault_value(getGoods_attrs.get(i).getDefault_value());
					productIndexBeanList.add(productIndexBean);

				}

			}
			ProductIndexListBeans productIndexListBeans = null;
			for (int i = 0; i < productIndexBeanList.size(); i++) {//构造符合条件的数据
				if (i == 0 || i % 2 == 0) {
					productIndexListBeans = new ProductIndexListBeans();
					productIndexListBeanses.add(productIndexListBeans);
				}
				productIndexListBeans.getBeans().add((productIndexBeanList.get(i)));
			}
		}
		productExtraInfAdapter.notifyDataSetChanged();
		//质检报告
		check_report.clear();
		check_report.addAll(auctionDetailBean.getRes().getCheck_report_info());

		inspectionReportForAuctionAdapter.notifyDataSetChanged();

		if ("1".equals(auctionDetailBean.getRes().getIs_ported())) {
			tvCanSplit.setVisibility(View.VISIBLE);
		} else {
			tvCanSplit.setVisibility(View.GONE);
		}

	}

	@Override
	protected void initData() {
		auctionId = getIntent().getStringExtra("auctionId");
	//	Toast.makeText(this, " auctionId=" + auctionId, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);

		tvGoodsName = (TextView) findViewById(R.id.tv_goods_name);
		tvCanSplit = (TextView) findViewById(R.id.can_split_tv);
		tvAmount = (TextView) findViewById(R.id.tv_amount);
		tvAmountUnit = (TextView) findViewById(R.id.tv_amount_unit);
		tvGoodsPrice = (TextView) findViewById(R.id.tv_goods_price);
		tvOfferTimes = (TextView) findViewById(R.id.tv_offer_times);
		tvStartPrice = (TextView) findViewById(R.id.tv_start_price);
		tvDeliveryType = (TextView) findViewById(R.id.tv_delivery_type);
		tvAuctionMode = (TextView) findViewById(R.id.tv_auction_mode);
		tvTransportMethod = (TextView) findViewById(R.id.tv_transport_method);
		tvGoodsType = (TextView) findViewById(R.id.tv_goods_type);
		tvPayMethod = (TextView) findViewById(R.id.tv_pay_method);
		tvMarkupPrice = (TextView) findViewById(R.id.tv_markup_price);
		linearlayoutDealPrice = (LinearLayout) findViewById(R.id.linearlayout_deal_price);
		tvDealPrice = (TextView) findViewById(R.id.tv_deal_price);
		cdll = (CountDownLinearLayout) findViewById(R.id.cdll);
		productIndexListView = (MyListView) findViewById(R.id.listview_attrs);
		//productIndexGridView = (MyGridView) findViewById(R.id.gridView_attrs);
		inspectionReportGridView = (MyGridView) findViewById(R.id.gridView1);
		listviewAucList = (MyListView) findViewById(R.id.listview);
		btnCollect = (Button) findViewById(R.id.btn_collect);
		orderBtn = (Button) findViewById(R.id.order_btn);

		mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);
		mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				onRefreshGetData();
			}

		});
	}
	private void onRefreshGetData() {
		//获取拍卖详情
		getAuctionDetails(false);
		//获取拍卖记录
		getAuctionOrderList(false);
		if (ConstantClass.state == 1) {//如果已经登录检查是否收藏
			checkCollect(false);
		}
	}

	private void getAuctionOrderList(boolean flag) {
		//获取出价记录 {"auctionId":"39","thisPage":"1","pageNum":"10"}
		AuctionLogListSubmitBean auctionLogListSubmitBean = new AuctionLogListSubmitBean();
		auctionLogListSubmitBean.setAuctionId(auctionId);
		auctionLogListSubmitBean.setThisPage("1");
		auctionLogListSubmitBean.setPageNum("300");
		AuctionLogListModel auctionLogListModel = new AuctionLogListModel(this, new OnNetBackListener<AuctionLogListBean>() {
			@Override
			public void netBack(AuctionLogListBean data) {
				if (data == null) {
					Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						autionOrderList.clear();
						autionOrderList.addAll(data.getRes().getList());

						auctionOrderListAdapter.notifyDataSetChanged();

					} else {
						//Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		auctionLogListModel.request(JSONUtil.toJSON(auctionLogListSubmitBean), flag);

	}

	private void getAuctionDetails(boolean flag) {
		//获取拍卖详情
		AuctionDetailSubmitBean auctionDetailSubmitBean = new AuctionDetailSubmitBean();
		auctionDetailSubmitBean.setAuctionId(auctionId);
		AuctionDetailModel auctionDetailModel = new AuctionDetailModel(this, new OnNetBackListener<AuctionDetailBean>() {
			@Override
			public void netBack(AuctionDetailBean data) {
				if (data == null) {
					Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						auctionDetailBean = data;
						initViewData();
						//取消刷新状态
						mPullRefreshScrollView.onRefreshComplete();
					} else {
						Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		auctionDetailModel.request(JSONUtil.toJSON(auctionDetailSubmitBean), flag);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		orderBtn.setOnClickListener(this);
		btnCollect.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.btn_collect:
				if (ConstantClass.state == 0) {
					intent.setClass(this, LoginActivity.class);
					startActivity(intent);
					return;
				} else {
					if (isCollected) {//已经收藏 取消收藏
						removeCollect();
					} else {//未收藏 去收藏
						addCollect("auction", auctionId);
					}
				}
				break;
			case R.id.order_btn:
				if (ConstantClass.state == 0) {
					intent.setClass(this, LoginActivity.class);
					startActivity(intent);
					return;
				} else {
					intent = new Intent(AuctionDetailsActivity.this, AuctionOrderActivity.class);
					intent.putExtra("auctionId", auctionId);
					// intent.putExtra("buy_margin", auctionDetailBean.getRes().getBuy_margin()+"");
					// intent.putExtra("money", auctionDetailBean.getRes().getTotal_price());//需要修改 待定这里是用做下单前计算保证金  这里用的起拍价
					startActivity(intent);
				}


				break;
			default:
				break;
		}
	}

	private void removeCollect() {//取消收藏
		CancelCollectSubmitBean cancelCollectSubmitBean = new CancelCollectSubmitBean();
		cancelCollectSubmitBean.setId(collectId);

		new CancelCollectModel(this, new OnNetBackListener<CancelCollectBean>() {
			@Override
			public void netBack(CancelCollectBean data) {
				if (data == null) {
					Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {//成功取消
						isCollected = false;
						btnCollect.setText("收藏");
					} else {
						Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}).request(JSONUtil.toJSON(cancelCollectSubmitBean), true);
	}

	private void addCollect(String type, String objId) {
		new AddCollectModel(this, new OnNetBackListener<AddCollectBean>() {
			@Override
			public void netBack(AddCollectBean data) {
				if (data == null) {
					Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						//Toast.makeText(MallDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						checkCollect(true);
					} else {
						Toast.makeText(AuctionDetailsActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}).request(JSONUtil.toJSON(new AddCollectSubmitBean(type, objId)), true);

	}

	@Override
	protected void onDestroy() {
		cdll.stopTimer();
		super.onDestroy();
	}
}
